<script lang="ts" setup>
import { reactive, onMounted, onActivated } from 'vue'
import IdTimeForm from '@/components/form/IdTimeForm.vue'
import { useTable } from '@/hooks/useTable'
import { getToken } from '@/utils/cache/cookies'
import { useActivated } from '@/hooks/useActivated'
import { useRoute } from 'vue-router'
import { queryBikeRentRecord } from '@/api/bike'
import { queryBikeRentRecordCol, queryBikeRentRecordRequestData } from '@/api/bike/types/bike'
defineOptions({
  name: 'RentRecord'
})

const route = useRoute()
const form = reactive<queryBikeRentRecordRequestData>({
  bikeCode: (route.query.bikeCode as string) || '',
  startTime: '',
  endTime: '',
  bikeId: (route.query.bikeId as string) || ''
})

const { data, pagination, handleCurrentChange, handleSizeChange, load, loading } =
  useTable<queryBikeRentRecordCol>(queryBikeRentRecord, getToken() ?? '', () => form)

onMounted(() => {
  load()
})
onActivated(() => {
  form.bikeCode = (route.query.bikeCode as string) || ''
  form.bikeId = (route.query.bikeId as string) || ''
})

/** 处理是否缓存 */
useActivated(
  form,
  { form },
  [
    { key: 'startTime', path: 'form.startTime' },
    { key: 'endTime', path: 'form.endTime' }
  ],
  load,
  []
)
/** 提交表单 */
const handleSubmit = () => {
  load()
}
</script>

<template>
  <div class="xy-container">
    <id-time-form v-model="form" @submit="handleSubmit" />
    <el-table :data="data" v-loading="loading">
      <el-table-column prop="rentUser" label="车辆租赁人" sortable />
      <el-table-column prop="rentPeriod" label="租赁周期" />
      <el-table-column prop="startTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间">
        <template #default="scope">
          <span v-if="scope.row.endTime == '租赁中...'" style="color: var(--xy-warning-color)">{{
            scope.row.endTime
          }}</span>
          <span v-else>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
  </div>
</template>

<style scoped></style>
